#ifndef RASTER_H
#define RASTER_H

#include "predicate.h"

class Cell
{
public:
	Point p;

	Cell()
	{
		Cell(Point());
	}

	Cell(const Point &p_): p(p_.floor()) {}

	Cell(const Cell &c): p(c.p) {}

	bool operator<(const Cell &c) const
	{
		return p < c.p;
	}
};

class cell_iterator: public std::iterator<std::forward_iterator_tag, const Cell>
{
private:
	Cell beg, end;
	Cell current;
	Point beg_point, end_point;
	bool down;
public:
	bool finished, is_end;

	cell_iterator(): is_end(true)
	{}

	cell_iterator(const Point &beg_, const Point &end_): beg(beg_), current(beg_),
		end(end_), beg_point(beg_), end_point(end_), finished(false), is_end(false) 
	{
		down = beg.p.y > end.p.y;
	}

	bool operator==(const cell_iterator &i) const
	{
		return is_end ? (i.is_end ? true : i.finished) : (i.is_end ? finished : false);
	}

	bool operator!=(const cell_iterator &i) const
	{
		return !((*this) == i);
	}

	iterator& operator++()
	{
		if (down)
		{
			Point temp = Point(current.p.x + 1, current.p.y);
			int turn = left_turn(beg_point, end_point, temp);
			if (turn > 0) 
			{
				current.p.y--;
			}
			else
			{
				current.p.x++;
			}
		}
		else
		{
			Point temp = Point(current.p.x + 1, current.p.y + 1);
			int turn = left_turn(beg_point, end_point, temp);
			
			if (turn >= 0)
			{
				current.p.x++;
			}

			if (turn <= 0)
			{
				current.p.y++;
			}
		}

		finished = end < current;

		return (*this);
	}

	const Cell& operator*() const
	{
		return current;
	}
};

#endif